<template>
	<view class="parent">
		<view class="login-title">{{loginTitle}}</view>
		
		<view style="margin: 50rpx;">
			<view class="login-input" style="margin-top: 80rpx;">
				<input class="et" placeholder="请输入手机号" v-model="account" />
			</view>
			
			<view class="login-input" v-if="2==loginType">
				<input class="et" placeholder="请输入验证码" v-model="code" />
				<zy-button-code :account='account'></zy-button-code>
			</view>
			
			<view class="login-input" v-if="1==loginType">
				<input class="et" placeholder="请输入密码" v-model="password" password/>
			</view>
			
			<zy-button text="立即登录" @click.native="requestLogin" style="margin-top: 150rpx;"></zy-button>
			<view class="register">
				<view class="textButton" @click="loginTypeChange">{{loginTypeTitle}}</view>
			</view>
			<zy-privacy ref="privacy"></zy-privacy>
		</view>
		
	</view>
</template>

<script>
	import LoginUtil from '@/main/common/LoginUtil.js'
	import RouteUtil from '@/main/common/RouteUtil.js'
	export default {
		data() {
			return {
				loginType: '2',
				loginTitle: '手机号登录/注册',
				loginTypeTitle: '使用密码登录',
				account: '13950104779',
				password: '123456',
				code: '708090',
			}
		},
		methods: {
			loginTypeChange() {
				if (1 == this.loginType) {
					this.loginType = 2;
					this.loginTitle = '手机号登录/注册';
					this.loginTypeTitle = '使用密码登录';
				} else {
					this.loginType = 1;
					this.loginTitle = '密码登录/注册';
					this.loginTypeTitle = '验证码登录';
				}
			},
			goRegister() {
				RouteUtil.toRegister()
			},
			requestLogin() {
				if (!this.$refs.privacy.value()) {
					uni.$toast.showToast('请勾选隐私政策')
					return
				}
				let param = {
					loginName: this.account
				};
				if (1 == this.loginType) {
					param['passWord'] = this.password;
				} else {
					param['smsCode'] = this.code;
				}

				uni.$http.post('/api/login', param)
					.then(res => {
						LoginUtil.saveToken(res.data)
						uni.$http.get('/api/getLoginUser')
							.then(res => {
								LoginUtil.login(res)
							})
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.parent {
		// height: 100vh;
		padding: 0 50rpx;

		.login-title {
			color: #fff;
			font-size: 36rpx;
			font-weight: 500;
			margin-top: 260rpx;
		}

		.login-input {
			display: flex;
			flex-direction: row;
			align-items: center;
			background-color: rgba(200, 200, 200, 0.3);
			border-radius: 10rpx;
			height: 80rpx;
			margin-top: 50rpx;
			.et {
				padding: 0 30rpx;
				font-size: 28rpx;
				flex: 1;
				color: #fff;
			}
		}

		.register {
			display: flex;
			flex-direction: row;
			justify-content: center;
			align-items: center;

			.textButton {
				height: 50rpx;
				line-height: 50rpx;
				padding: 20rpx 20rpx;
				color: #fff;
				font-size: 24rpx;
			}
		}
	}
</style>